package com.example.sonone.service.impl;

import com.example.sonone.dao.TestDao;
import com.example.sonone.dao.TestDao2;
import com.example.sonone.pojo.Stu;
import com.example.sonone.service.TestService;
import com.example.sonone.service.TestService2;
import com.example.sonone.util.ExcelUtil;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private TestDao testDao;

    // 启动的时候就打印了
    @PostConstruct
    public void init() {
        System.out.println("============= PostConstruct============================");
    }

    /**
     * 倪经理 你是真的菜
     */
    @Override
    public void exportExcel(HttpServletResponse servletResponse) {
        // 1.定义列名 这尼玛菜草
        List<String> columnName = new ArrayList<>();

        columnName.add("提报人");
        columnName.add("问题描述");
        columnName.add("问题结果");
        columnName.add("问题类型");


        //2, 定义数据结果集  真他麻痹的彩笔 草
        List<Map<String, String>> data = new ArrayList<>();
        List<Map<String, String>> maps = testDao.excelList();
        for (Map<String, String> params : maps) {
            Map<String, String> map = new HashMap<>();
            map.put("提报人", params.get("staffname"));
            map.put("问题描述", params.get("questionCollectContent"));
            map.put("问题结果", params.get("responseContent"));
            map.put("问题类型", params.get("questionCollectType"));
            data.add(map);
        }

        //3 查找需要合并的单元格行数
        List<Integer> mergeRow = testDao.getRow();

        ExcelUtil excelUtil = ExcelUtil.getInstance();
        excelUtil.dataToExcel("倪经理真几把菜", data, "菜的不忍直视", columnName, mergeRow, servletResponse);
    }

    @Override
    public void exportExcel2(HttpServletResponse servletResponse, String month) {


        // 1.定义列名
        List<String> columnName = new ArrayList<>();
        columnName.add("省份");
        columnName.add("本月冻结车辆次数");
        columnName.add("本月解冻车辆次数");
        columnName.add("本月冻结企业次数");
        columnName.add("本月解冻企业次数");

        //2, 查数据
        List<Map<String, Object>> truckList = testDao.getTruckCountByMonth(month);

        List<Map<String, Object>> unitList = testDao.getUnitCountByMonth(month);

        truckList.stream().forEach(n -> {
            String provinceIdN = MapUtils.getString(n, "provinceId");
            unitList.stream().forEach(m -> {
                String provinceIdM = MapUtils.getString(m, "provinceId");
                if (StringUtils.equalsIgnoreCase(provinceIdN, provinceIdM)) {
                    n.put("freezeUnitCount", MapUtils.getIntValue(m, "freezeUnitCount"));
                    n.put("unFreezeUnitCount", MapUtils.getIntValue(m, "unFreezeUnitCount"));
                }
            });
        });

        // 3，定义结果集返回
        List<Map<String, String>> data = new ArrayList<>();
        truckList.stream().forEach(n -> {
            Map<String, String> map = new HashMap<>();
            map.put("省份", MapUtils.getString(n, "provinceName"));
            map.put("本月冻结车辆次数", String.valueOf(MapUtils.getIntValue(n, "freezeTruckCount")));
            map.put("本月解冻车辆次数", String.valueOf(MapUtils.getIntValue(n, "unFreezeTruckCount")));
            map.put("本月冻结企业次数", String.valueOf(MapUtils.getIntValue(n, "freezeUnitCount")));
            map.put("本月解冻企业次数", String.valueOf(MapUtils.getIntValue(n, "unFreezeUnitCount")));
            data.add(map);
        });

        ExcelUtil excelUtil = ExcelUtil.getInstance();
        excelUtil.dataToExcel2("预约平台各省冻结解冻表", data, "各省冻结解冻", columnName, month, servletResponse);


    }


    @Autowired
    private TestService2 testService2;

    @Autowired
    private TestDao2 testDao2;

    @Override
    @Transactional
    public void insert(Map<String, String> map) {
//        testDao.inset(map);
        testDao.insetShiwu();
        testService2.insert(null);
        int i = 1 / 0;

    }

    @Override
    public void save(List<Stu> cachedDataList) {
        System.out.println(cachedDataList);
    }

    @Autowired
    private TestServiceImpl testServiceImpl;


    @Transactional
    public void A() {
        testDao.insetShiwu();
        try {

            testServiceImpl.B();
//            System.out.println(1 / 0);// 这种情况不会回滚
        } catch (Exception e) {
            System.out.println("1211111111111111111111111");
            throw e;
//            e.printStackTrace();
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void B() {
        testDao2.insetShiwu2();
        System.out.println(1 / 0); // 这种情况 会回滚
    }


}
